package com.tairanchina.md.meeting.service.impl;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.dao.YearMeetingUserMapperExt;
import com.tairanchina.md.meeting.entity.YearMeetingUser;
import com.tairanchina.md.meeting.entity.YearMeetingUserExample;
import com.tairanchina.md.meeting.service.YearMeetingUserService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lxliuxuan on 2016/12/24.
 */
@Transactional
@Service
public class YearMeetingUserServiceImpl implements YearMeetingUserService {

	Logger logger = LoggerFactory.getLogger(YearMeetingUserServiceImpl.class);
	@Resource
	private YearMeetingUserMapperExt yearMeetingUserMapperExt;
	
	@Override
	public List<YearMeetingUser> findAllPrizeUser() {
		List<YearMeetingUser> list = new ArrayList<>();
		try {
			YearMeetingUserExample example = new YearMeetingUserExample();
			example.createCriteria().andIsSignEqualTo(MeetingConstants.COMMON_ONE).andIsAwardEqualTo(MeetingConstants.COMMON_ZERO);
			list = yearMeetingUserMapperExt.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询可以参与年会抽奖用户异常",e);
		}
		
		return list;
	}

	@Override
	public void updateUser(YearMeetingUser yearMeetingUser) {

		yearMeetingUserMapperExt.updateByPrimaryKeySelective(yearMeetingUser);

	}


	@Override
	public YearMeetingUser findUserByPhone(String phone) {

		YearMeetingUserExample example = new YearMeetingUserExample();
		YearMeetingUserExample.Criteria criteria = example.createCriteria();
		criteria.andUserPhoneEqualTo(phone);
		List<YearMeetingUser> yearMeetingUsers = yearMeetingUserMapperExt.selectByExample(example);
		if(yearMeetingUsers !=null && yearMeetingUsers.size()>0){
			return yearMeetingUsers.get(0);
		}

		return null;
	}

	@Override
	public Integer getSignNum() {
		Integer signNum = 0;
		YearMeetingUserExample example = new YearMeetingUserExample();
		YearMeetingUserExample.Criteria criteria = example.createCriteria();
		criteria.andIsSignEqualTo(1);
		signNum = yearMeetingUserMapperExt.countByExample(example);
		return signNum;
	}

}
